
	var member ;
	var order = {};
	
	var orderItem = [];
	var proTotal = 0;
	var buyNum = 0;
	var freight=0;  //运费
	
	var cart;
	var productList =[];
	
	
	apiready = function() {
        
        cart=api.pageParam.cart;   // 购物车数据
      
	    member = $api.getStorage('member');  //会员
	    	
	    
		//显示购买的商品
		showBuyData();
		
		calculation();  //计算费用


		//监听选择收货地址
		api.addEventListener({
		    name: 'selectaddress'
		}, function(ret){
		    if(ret && ret.value){
		        showaddress(ret.value);
		    }
		});
		
		
	}
	
	//打开选择地址
	function openaddress(){
	     api.openWin({
	        name: 'address',
	        url: 'address.html',
	        bounces: false,
	        vScrollBarEnabled:false,
	        hScrollBarEnabled:false,
	       }
	    );
	};
	
	
	//显示收货信息
	function showaddress(address){
	    
	    
	    var currAddressDetail=address.province+address.city+address.district+address.address;
	    $api.text($api.byId('receiver'), address.receiver);
	    $api.text($api.byId('mobile'), address.mobile);
	    $api.text($api.byId('address'), currAddressDetail);
	    
	    
	    
	    $api.css($api.byId('noaddress'),'display:none');
	    $api.css($api.byId('yesaddress'),'display:block');
	    
	    order.receiver=address.receiver;
	    order.mobile=address.mobile;
		order.province=address.province;
		order.city=address.city;
		order.district=address.district;
		order.currAddressDetail=currAddressDetail;
	    
	}
	

	
	//购买的商品
	var showBuyData = function() {
		api.showProgress({
	        title: '加载中...',
	        modal: false
	    });
	  	 productList=cart.item;
         var html = '';
         for(var i=0;i<productList.length;i++){
            html+='<div class="am-u-sm-12" style="padding: 0px;">';
	            html+='<div class="am-u-sm-3" style="padding:8px 0px;">';
	            html+='<img src="'+serviceUrl+'/files/'+(productList[i].proDefImg||0)+'" style="width:80px;height: 80px"/>';
	            html+='</div>';
	            html+='<div class="am-u-sm-9" style="padding:5px 0px">';
		            html+='<div class="am-text-xs am-text-truncate" style="color:black">'+productList[i].proName+'</div>';
		            html+='<div style="color:red;font-size:15px;">￥'+productList[i].price.toFixed(2)+'</div>';
		            html+='<div>';
			            html+='<div class="aui-counter">';
				            html+='<div class="aui-counter-minus" onclick="reduceItem(\''+ productList[i].id +'\');"></div>';
				            html+='<input class="aui-counter-input" readonly id="good_num_'+ productList[i].id +'" type="text" value="'+productList[i].num+'">';
				            html+='<div class="aui-counter-plus" onclick="addItem(\''+ productList[i].id +'\');"></div>';
			            html+='</div>';
	               html+='</div>';
               
               html+='</div>';
            html+='</div>';
			
		}
	
		$api.html($api.byId('goodList'), html);
		
		//alert(html)
	
		api.hideProgress();
	}
	
	
	
	
	

	//提交订单
	var addOrder = function() {
	        
		   if(order.receiver==undefined){
		    	 toast('请选择收货人');
				 return;
		    }
		    
		    
		    for(var i=0;i<productList.length;i++){
		        orderItem.push({
		             'id' : productList[i].id,
			    	 'num' :  productList[i].num,
			    	 'price' : productList[i].price,
			    	 'total' :(productList[i].price*productList[i].num).toFixed(2),
			    	 'proDefImg' : productList[i].proDefImg,
			    	 'proName' : productList[i].proName,
			    	 'merName' :productList[i].merName,
			    	 'purprice' : productList[i].purprice,
			    	 'code' : productList[i].code,
		        });
		    }
		    
		
				
			order.item = orderItem;
			order.payType=$api.val($api.dom('input[name="payType"]:checked'));
			order.memberAccount = member.account;
			order.remarks=$api.val($api.byId('remarks'));
			order.recman=$api.val($api.byId('recman'));
			order.source=api.deviceName;
			order.buytype='普通购买';
			
			//alert(JSON.stringify(orderItem));
		    
		   // return;
		
	
            do_ajax('/app/member/addorder2','post',order,function(respose,err){
				if (respose.resultCode == "0") {
		                api.hideProgress();
						var orderinfo= {
							orderNo: respose.orderNo,
							total: order.total,
							payType: order.payType,
						};
						
						api.openWin({
					        name: 'pay',
					        url: "../pay.html",
					        pageParam: {orderinfo:orderinfo},
					       }
					    );
					    
					 //  openWin('pay',{orderinfo:orderinfo});

				} else
					alert(respose.datas);
			});
		
	}
	

	/*
	 * 加法
	 */
	function addItem(id){
	
	  
	    var index = findProduct(id);
	    
		if(index != -1) {
			++productList[index].num;
			$api.val($api.byId('good_num_'+id),productList[index].num);
		}
		calculation();
	};

	/*
	 * 减法
	 */
	function reduceItem(id){
	    var index = findProduct(id);
	    
    
		var item = productList[index];
		if(item.num > 1){
			--item.num;
			$api.val($api.byId('good_num_'+id),item.num);
		} 
	    calculation();
		
	};
	

    function findProduct(id){
	    return findIndex(productList,id);
	};
	
	
	
	/**
	 * 计算费用
	 */
	var calculation = function() {

	    var totalprice = 0;
		var totalNum = 0;
		var price_countFreight = 0;	//要算运费的总价
	
		for(var i = 0; i < productList.length; i++){
			var product = productList[i];
			
			product.total = product.num * product.price;
			totalprice += product.total;
		    totalNum += product.num;      //购买数量
		    
		    if(product.isfreefreight != '是') {
		    	
		    	price_countFreight += product.total;
		    }
		}
		
		order.buyNum = totalNum;  //购买数量
		
		if(price_countFreight==0){
	       freight=0;
	    }
	    
		if(price_countFreight<88){
	       freight=12;
	    }
	    
	    if(price_countFreight>88){
	        freight=0;
	    }
	    
	    if(price_countFreight>500){
	        freight=-20;
	    }
	    
	    order.proTotal=totalprice.toFixed(2);
	    order.freight=freight;
	    order.total=(totalprice+freight).toFixed(2);
	    order.rTotal=(totalprice+freight).toFixed(2);
	    
	    
	    $api.text($api.byId('proTotal'), order.proTotal);
	    $api.text($api.byId('freight'), order.freight);
	    $api.text($api.byId('total'), order.total);
	    

	}
	
	